<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 视频评论 回复 模型
 * @package app\user_center\model
 */
class VideoPlayCommentReplay extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_comment_reply';
	
	use SoftDelete;
    protected $deleteTime = 'reply_status';
    protected $defaultSoftDelete = 0;
    
	public static function init()
    {
        self::event('after_insert', function ($user) {
            //评论的回复数增加
            model('VideoPlayComment') -> incReplyTimes($user->comment_id);
        });
        self::event('after_delete', function ($user) {
            //评论的回复数减少
            model('VideoPlayComment') -> decReplyTimes($user->comment_id);
        });
    }
    /**
     * 添加数据
     * @param int $uid 用户id
     * @param int $play_id 视频id
     * @param int $comment_id 评论id
     * @param int $r_uid 	 回复了谁
     * @param int $content 评论内容
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function addData($uid, $play_id, $comment_id, $r_uid, $content){
		$video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
		$play_id = $video_info['data'];//关联的视频id	
		
        $this -> uid             = $uid;
        $this -> play_id         = $play_id;
        $this -> comment_id      = $comment_id;
        $this -> r_uid      	 = $r_uid;
        $this -> content         = $content;
        $res = $this -> isUpdate(false) -> save();
        if($res){
            return array_out(200, 'success', $this -> id);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 删除数据
     * @param int $reply_id 回复id
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function deleteData($reply_id){
		
        $res = self::destroy($reply_id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 点赞数增加
     * @param int $reply_id 评论id
     * @param int $times 数量（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function incSupportTimes($reply_id, $times = 1){
        $map = [];
        $map['id'] = $reply_id;
        $res = $this -> where($map) -> setInc('support_times', $times);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 点赞数减少
     * @param int $reply_id 评论id
     * @param int $times 数量（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function decSupportTimes($reply_id, $times = 1){
        $map = [];
        $map['id'] = $reply_id;
        $res = $this -> where($map) -> setDec('support_times', $times);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取视频评论 回复列表
     * @param int $comment_id 视频id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getListByCommentid($comment_id, $page = 1){
        $map = [];
        $map['comment_id'] = $comment_id;
        $list = $this -> withJoin('getUcMember', 'LEFT') -> where($map) -> order('support_times desc,create_time desc')  -> paginate();
        return array_out(200, 'success', $list);
    }

    /**
     * 根据 id 获取回复数据
     * @param int $comment_id 视频id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getDataById($replyid, $cache = true){
        $info = $this -> cache($cache) -> find($replyid);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 用户信息 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }

    /**
     * 用户信息 关联载入（一对一）
     */
    public function getUcMemberReply(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'r_uid');
    }

    /**
     * 视频信息 关联载入（一对一）
     */
    public function getVideoPlay(){
        return $this -> hasOne('app\video\model\VideoPlay','id', 'play_id');
    }

    /**
     * 评论信息 关联载入（一对一）
     */
    public function getVideoPlayComment(){
        return $this -> hasOne('app\video\model\VideoPlayComment','id', 'comment_id');
    }
}
